package com.hangxintong.common.base.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lvyanfei
 * @date 2020-12-117
 */
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
@Setter
@ToString
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "updatedAt"}, ignoreUnknown = true)
public abstract class BaseJpaModel implements Serializable, IEntity {

    private static final long serialVersionUID = -4395135018894432198L;

    /**
     * 分钟的毫秒数
     */
    public static final long MINU_MILLIS = 60 * 1000;

    /**
     * 小时的毫秒数
     */
    public static final long HOUR_MILLIS = 60 * MINU_MILLIS;

    /**
     * 天的毫秒数
     */
    public static final long DATE__MILLIS = 24 * HOUR_MILLIS;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @CreatedDate
    @Column(name = "created_at", insertable = true, updatable = false)
    @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
    private Date createdAt;

    @Column(name = "updated_at", insertable = false, updatable = true)
    @JsonFormat(pattern="yyyy-MM-dd",timezone = "GMT+8")
    @LastModifiedDate
    private Date updatedAt;

    @Column(name = "created_by", insertable = true, updatable = false)
    private String createdBy;

    @Column(name = "updated_by", insertable = false, updatable = true)
    private String updatedBy;

    @Override
    public int hashCode() {
        if (this.id != null) {
            return 31 * this.getClass().hashCode() + 17 * this.id.hashCode();
        }
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;

        final BaseJpaModel other = (BaseJpaModel) obj;
        if (this.id == null) {
            if (other.id != null) {
                return false;
            } else { // 双方都是新建对象，只能用JDK默认方式比较
                return super.equals(obj);
            }
        } else {
            return id.equals(other.id);
        }
    }

    /**
     * 把 字符串 List 转化为特殊的逗号字符串
     * 注意： 列表内的字符串不应该有逗号
     *
     * @param list
     * @return
     */
    public String listToComma(List<String> list) {
        if (list == null || list.size() == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder(",");
        list.forEach(s -> {
            if (s != null && s.length() > 0) {
                sb.append(s.trim()).append(",");
            }
        });
        return sb.toString();
    }

    /**
     * 把特定的逗号字符串转换成字符串列表
     *
     * @param comma
     * @return
     */
    public List<String> commaToList(String comma) {
        List<String> rst = new ArrayList<>();
        if (comma != null) {
            String[] commaArray = comma.split(",");
            for (String s : commaArray) {
                if (s != null && !"".equals(s)) {
                    rst.add(s);
                }
            }
        }
        return rst;
    }
}

